﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;

public class ButtonMultiple : Button
{
	public bool m_Disabled;
	public List<ButtonEvent> m_Events = new List<ButtonEvent>();

	public enum ButtonState
	{
		Normal = 0,
		Highlighted = 1,
		Pressed = 2,
		Disabled = 3,
	}

	protected override void Awake()
	{
		base.Awake();

		transition = Transition.None;

		foreach (ButtonEvent evt in m_Events)
		{
			evt.m_InitialScale = evt.m_MainObj.transform.localScale;
		}
	}

	public override void OnPointerEnter(UnityEngine.EventSystems.PointerEventData eventData)
	{
		base.OnPointerEnter(eventData);

		if (!m_Disabled)
		{
			foreach (ButtonEvent evt in m_Events)
			{
				evt.ApplyEvent(ButtonState.Highlighted);
			}
		}
	}

	public override void OnPointerExit(UnityEngine.EventSystems.PointerEventData eventData)
	{
		base.OnPointerExit(eventData);
		
		if (!m_Disabled)
		{
			foreach (ButtonEvent evt in m_Events)
			{
				evt.ApplyEvent(ButtonState.Normal);
			}
		}
	}

	public override void OnPointerDown(UnityEngine.EventSystems.PointerEventData eventData)
	{
		base.OnPointerDown(eventData);
		
		if (!m_Disabled)
		{
			foreach (ButtonEvent evt in m_Events)
			{
				evt.ApplyEvent(ButtonState.Pressed);
			}
		}
	}

	public override void OnPointerUp(UnityEngine.EventSystems.PointerEventData eventData)
	{
		base.OnPointerUp(eventData);
		
		if (!m_Disabled)
		{
			foreach (ButtonEvent evt in m_Events)
			{
				evt.ApplyEvent(ButtonState.Normal);
			}
		}
	}

	public override void OnPointerClick(UnityEngine.EventSystems.PointerEventData eventData)
	{
		if (!m_Disabled)
		{
			base.OnPointerClick(eventData);
			onClick.Invoke();
		}
	}

	public void Disable(bool _disabled)
	{
		m_Disabled = _disabled;

		foreach (ButtonEvent evt in m_Events)
		{
			evt.ApplyEvent(m_Disabled ? ButtonState.Disabled : ButtonState.Normal);
		}
	}

	[System.Serializable]
	public class ButtonEvent
	{
		public GameObject m_MainObj;
		public Vector3 m_InitialScale = Vector3.zero;

		public bool m_ChangeColor = true;
		public bool m_SwapSprite;
		public bool m_PunchScale;
		public bool m_UseAnimator;

		public Color[] m_Colors;
		public Sprite[] m_Sprites;
		public float[] m_PunchValues;
		public string[] m_Triggers;

		// Used by editor
		public bool m_Toggle = true;

		public ButtonEvent(GameObject _obj)
		{
			m_MainObj = _obj;

			Graphic _graph = m_MainObj.GetComponent<Graphic>();
			if(_graph != null)
			{
				Color _color = _graph.color;
				m_Colors = new Color[]{_color, _color, _color, _color};
			}

			Image _img = m_MainObj.GetComponent<Image>();
			if(_img != null)
			{
				Sprite _sprite = _img.sprite;
				m_Sprites = new Sprite[]{_sprite, _sprite, _sprite, _sprite};
			}

			m_PunchValues = new float[]{1f, 1f, 1f, 1f};
		}
		
		public void ApplyEvent(ButtonState _state)
		{
			if (m_ChangeColor)
			{
				m_MainObj.GetComponent<Graphic>().color = m_Colors[(int)_state];
			}
			
			if (m_PunchScale)
			{
				if (m_InitialScale == Vector3.zero)
				{
					m_InitialScale = Vector3.one;
				}

				if (_state == ButtonState.Pressed || _state == ButtonState.Highlighted)
				{
					m_MainObj.transform.localScale = m_InitialScale * m_PunchValues[(int)_state];
				}
				else
				{
					m_MainObj.transform.localScale = m_InitialScale;
				}
			}
			
			if (m_SwapSprite)
			{
				m_MainObj.GetComponent<Image>().sprite = m_Sprites[(int)_state];
			}

			if(m_UseAnimator)
			{
				m_MainObj.GetComponent<Animator>().SetTrigger(m_Triggers[(int)_state]);
			}
		}
	}
}
